package com.juc.concurrency.basic.join;

/**
 * @author huangweiyue
 * @description Thread.join方法 等待
 * @date Created in 2021-07-30
 */
public class JoinExample {
    public static void main(String[] args) throws InterruptedException {
        Thread threadOne = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("child threadOne over!");
        });

        Thread threadTwo = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("child threadTwo over!");
        });

        //启动子线程
        threadOne.start();
        threadTwo.start();

        System.out.println("wait all child thread over");

        //主线程会在子线程调用join后被阻塞，等待子线程执行完毕后返回（其实CountdownLatch也具有这个效果）
        threadOne.join();
        threadTwo.join();

        System.out.println("all child thread over");
    }
}
